<?php 
namespace App\Models;

use Illuminate\Database\Eloquent\Model;


class Collections extends Model {

	protected $table = 'collections';
        
	protected $fillable = array('user_id', 'name', 'description', 'images', 'record', 'address', 'addtoGallery');
        
        //取自己的作品函数，不可用于取别人的作品
        public function getSelfCollection($collection_id,$user_id){
            if(!$collection = $this->where('id', $collection_id)->where('user_id', $user_id)->first()){
                return false;            
            }
            $collection->images = json_decode($collection->images);
            //防止错误数据类型导致客户端崩溃
            if (!$collection->images) {
                $collection->images = '[]';
                $collection->images = json_decode($collection->images);
            }
            return $collection;
        }
        
        public function getCollectionById($collection_id){
            if(!$collection = $this->where('id', $collection_id)->first()){
                return false;            
            }
            $collection->images = json_decode($collection->images);
            //防止错误数据类型导致客户端崩溃
            if (!$collection->images) {
                $collection->images = '[]';
                $collection->images = json_decode($collection->images);
            }
            return $collection;
        }

}